package com.flyou.ccnucenter.ui.activity.henuBeside.map;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;


public class MapActivity extends BaseActivity1 implements View.OnClickListener {
    private MapView mMapView = null;
    private RelativeLayout mTitlebarRlBack;
    private TextView mTitlebarTvTitle;
    private View markView;
    private LatLng myLocation;
    private Intent intent;
    private String name;
    private String address;
    private double latitude;
    private double longitude;
    private int distance;

    @Override
    protected void initView() {
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_map);
        mMapView = (MapView) findViewById(R.id.bmapView);
        mTitlebarRlBack = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        mTitlebarTvTitle = (TextView) findViewById(R.id.titlebar_tv_title);
        // 获取地图对象
        BaiduMap mMapViewMap = mMapView.getMap();
        // 设置当前点坐标
        // TODO: 2015/11/2  设置活动位置的坐标
        intent = getIntent();
        latitude = intent.getDoubleExtra("aim_lat", 34.82336);
        longitude = intent.getDoubleExtra("aim_lng", 114.31496);
        myLocation = new LatLng(latitude, longitude);

        //定义地图状态
        MapStatus mMapStatus = new MapStatus.Builder()
                .target(myLocation)
                .zoom(18)
                .build();
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        //改变地图状态
        mMapViewMap.setMapStatus(mMapStatusUpdate);

        //构建Marker图标
        String keyWord = intent.getStringExtra("keyWord");
        BitmapDescriptor bitmap;
        switch (keyWord) {
            case "美食":

                bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.map_food);
                break;
            case "超市":

                bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.map_mall);
                break;
            case "KTV":

                bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.map_ktv);
                break;
            case "美妆":

                bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.map_beautiful);
                break;
            case "酒店":

                bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.map_hotel);
                break;
            case "ATM":

                bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.map_atm);
                break;
            case "电影":

                bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.map_movie);
                break;
            case "网吧":

                bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.map_cyberbar);
                break;

            default:
                bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.map_food);
                break;
        }


        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(myLocation)
                .icon(bitmap);

        //在地图上添加Marker，并显示
        mMapViewMap.addOverlay(option);
        mMapViewMap.setOnMarkerClickListener(mMapListener);
    }

    @Override
    protected void initDate() {
        name = intent.getStringExtra("name");
        address = intent.getStringExtra("address");
        distance = intent.getIntExtra("distance", 0);
        mTitlebarTvTitle.setText(name);
    }

    @Override
    protected void setListener() {
        mTitlebarRlBack.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titlebar_rl_back:
                finish();
                break;
        }
    }

    //    地图 Marker 覆盖物点击事件监听接口：
    BaiduMap.OnMarkerClickListener mMapListener = new BaiduMap.OnMarkerClickListener() {
        /**
         * 地图 Marker 覆盖物点击事件监听函数
         * @param marker 被点击的 marker
         */
        public boolean onMarkerClick(Marker marker) {
            markView = View.inflate(MapActivity.this, R.layout.view_map_dialog, null);
            TextView being_title = (TextView) markView.findViewById(R.id.being_title);
            TextView being_distance = (TextView) markView.findViewById(R.id.being_distance);
            TextView being_address = (TextView) markView.findViewById(R.id.being_address);
            being_title.setText(name);
            being_distance.setText("距你：" + distance + "m");
            being_address.setText("地址：" + address);
            AlertDialog.Builder dialog = new AlertDialog.Builder(MapActivity.this);

            dialog.setView(markView);
            dialog.show();
            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    markView = null;
                }
            });
            return true;

        }
    };
}
